﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Engine.Sgml.Validation {
    internal sealed class NodeIterator : IEnumerator<SElement> {
        private SElement current;

        public NodeIterator(SElement root) {
            Guard.ArgumentNotNull(root, "root");
            this.current = root;
            }
        public SElement Current {
            get { return current; }
            }

        public void Dispose() {
            current = null;
            }

        object System.Collections.IEnumerator.Current {
            get { return current; }
            }

        public bool MoveNext() {
            if (current == null)
                return false;
            if (current.Nodes.Count > 0) {
                current = current.FirstNode;
                return true;
                }
            SElement next = current.NextNode;
            if (next == null) {
                if (current.Parent == null) {
                    current = null;
                    return false;
                    }
                do {
                    current = current.Parent;
                    next = current.NextNode;
                    } while (next == null && current.Parent != null);
                if (next == null) {
                    current = null;
                    return false;
                    }
                }
            current = next;
            return true;
            }

        public void Reset() {
            throw new NotSupportedException();
            }
        }
    }
